//
//  MYGFriendSearchViewController.swift
//  MYDoctor
//
//  Created by 小火科技 on 2024/4/26.
//

import UIKit
import BRPickerView


class MYGFriendSearchViewController: MYGBaseController {
    var hosArr:[String] = []
    var keshiArr:[String] = []
    
    private var hospital = ""
    private var departMent = ""
    private var work = ""
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        getHosAndSub()
        configUI()
    }
    func configUI(){
        title = "添加好友"
        view.addSubviews([hospitalInput,deparmentInput,workInput,saveButton])
        
        hospitalInput.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(21)
            make.top.equalToSuperview().offset(20)
            make.height.equalTo(52)
        }
        
        deparmentInput.snp.makeConstraints { make in
            make.left.height.right.equalTo(hospitalInput)
            make.top.equalTo(hospitalInput.snp.bottom)
        }
        
        workInput.snp.makeConstraints { make in
            make.left.height.right.equalTo(hospitalInput)
            make.top.equalTo(deparmentInput.snp.bottom)
        }
        
        
        saveButton.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.bottom.equalToSuperview().inset(bottomSafeheight)
            make.height.equalTo(49)
        }
        
    }
    
    private lazy var hospitalInput : MYGInputView = {
        let perI = MYGInputView(title: "医院", subTitle: "不限",isShow: true, frame: .zero)
        perI.selelctView = { [weak self] in
            
            let vc = MYGHospitalViewController()
           vc.callHospitailName = {name in
               self?.hospitalInput.textFiled.text = name
               self?.hospital = name
           }
            self?.navigationController?.pushViewController(vc, animated: true)
            
        }
     
        return perI
    }()

    private lazy var deparmentInput : MYGInputView = {
        let perI = MYGInputView(title: "科室", subTitle: "不限",isShow: true, frame: .zero)
        perI.selelctView = { [weak self] in
            BRStringPickerView.showPicker(withTitle: "请选择科室", dataSourceArr: self?.keshiArr, select: 0) { [self] item in
                self?.deparmentInput.textFiled.text = item?.value
                self?.departMent = item?.value ?? ""
                
            }
        }
     
        return perI
    }()
    private lazy var workInput : MYGInputView = {
        let perI = MYGInputView(title: "职称", subTitle: "不限",isShow: true, frame: .zero)
        perI.selelctView = { [weak self] in
            BRStringPickerView.showPicker(withTitle: "请选择职称", dataSourceArr: ["不限","主任医师","副主任医师","主治医师","住院医师","医生助理"], select: 0) { [self] item in
                self?.workInput.textFiled.text = item?.value
                self!.work = item?.value ?? ""
                
            }
        }
     
        return perI
    }()
    
    private lazy var saveButton: UIButton = {
        let btn = UIButton()
        btn.setTitle("查找", for: .normal)
        btn.setTitleColor(.white, for: .normal)
        btn.titleLabel?.font = .medium(17)
        btn.backgroundColor = .rgba(73, 117, 236, 1)
        btn.addTarget(self, action: #selector(nextAction), for: .touchUpInside)
        return btn
    }()
    
}

extension MYGFriendSearchViewController {
    
    @objc func nextAction() {
        let vc = MYGFriendSearchResultViewController()
        vc.hospital = self.hospital
        vc.department = self.departMent
        vc.retitle = self.work
        self.navigationController?.pushViewController(vc)
        
    }
    
    func getHosAndSub(){
        
        MMYGNetwork<MYGResumeApi, MYGSubjectListModel>.request(target: .getSubjectList(params: [:])) {[weak self] result in
            guard let items = result?.data else {
                return
            }
            self?.keshiArr.append("不限")
            for item in items {
                self?.keshiArr.append(item.label)
            }
        } failed: { _, error in
//            MYGLoading.hide()
//            MYGHUD.showToast(text: error)
        }
    }
}
